package com.hiultra.archive.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hiultra.archive.domain.ArcArchive;
import com.hiultra.archive.domain.ArcBox;
import com.hiultra.archive.domain.ArchiveCabinet;
import com.hiultra.archive.service.IArcArchiveService;
import com.hiultra.archive.service.IArcBoxService;
import com.hiultra.common.annotation.Log;
import com.hiultra.common.constant.BusinessConstants;
import com.hiultra.common.constant.UserConstants;
import com.hiultra.common.core.controller.BaseController;
import com.hiultra.common.core.domain.AjaxResult;
import com.hiultra.common.core.page.TableDataInfo;
import com.hiultra.common.enums.BusinessStatus;
import com.hiultra.common.enums.BusinessType;
import com.hiultra.common.utils.ArcUtil;
import com.hiultra.common.utils.StringUtils;
import com.hiultra.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 档案盒管理Controller
 *
 * @author ls
 * @date 2022-07-28
 */
@Api(tags = "档案盒Controller")
@RestController
@RequestMapping("/base/box")
public class ArcBoxController extends BaseController {
    @Autowired
    private IArcBoxService arcBoxService;
    @Autowired
    private IArcArchiveService arcArchiveService;

    /**
     * 查询档案盒管理列表
     */
    @ApiOperation(value = "查询档案盒管理列表", notes = "查询档案盒管理列表")
    @PreAuthorize("@ss.hasPermi('base:box:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArcBox arcBox) {
        startPage();
        List<ArcBox> list = arcBoxService.selectArcBoxList(arcBox);
        return getDataTable(list);
    }

    /**
     * 导出档案盒管理列表
     */
    @ApiOperation(value = "导出档案盒管理列表", notes = "导出档案盒管理列表")
    @PreAuthorize("@ss.hasPermi('base:box:export')")
    @Log(title = "档案盒管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArcBox arcBox) {
        List<ArcBox> list = arcBoxService.selectArcBoxList(arcBox);
        ExcelUtil<ArcBox> util = new ExcelUtil<ArcBox>(ArcBox.class);
        util.exportExcel(response, list, "档案盒管理数据");
    }

    /**
     * 获取档案盒管理详细信息
     */
    @ApiOperation(value = "获取档案盒管理详细信息", notes = "获取档案盒管理详细信息")
    @PreAuthorize("@ss.hasPermi('base:box:query')")
    @GetMapping(value = "/{boxId}")
    public AjaxResult getInfo(@PathVariable("boxId") String boxId) {
        return AjaxResult.success(arcBoxService.selectArcBoxByBoxId(boxId));
    }

    /**
     * 新增档案盒管理
     */
    @ApiOperation(value = "新增档案盒管理", notes = "新增档案盒管理")
    @PreAuthorize("@ss.hasPermi('base:box:add')")
    @Log(title = "档案盒管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArcBox arcBox) {
        if (UserConstants.NOT_UNIQUE.equals(arcBoxService.checkBoxCodeUnique(arcBox))) {
            return AjaxResult.error("新增档案盒'" + arcBox.getBoxCode() + "'失败，档案盒号已存在");
        }

        return toAjax(arcBoxService.insertArcBox(arcBox));
    }

    /**
     * 修改档案盒管理
     */
    @ApiOperation(value = "修改档案盒管理", notes = "修改档案盒管理")
    @PreAuthorize("@ss.hasPermi('base:box:edit')")
    @Log(title = "档案盒管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArcBox arcBox) {
        if (UserConstants.NOT_UNIQUE.equals(arcBoxService.checkBoxCodeUnique(arcBox))) {
            return AjaxResult.error("修改档案盒'" + arcBox.getBoxCode() + "'失败，档案盒号存在");
        }
        return toAjax(arcBoxService.updateArcBox(arcBox));
    }

    /**
     * 删除档案盒管理
     */
    @ApiOperation(value = "删除档案盒管理", notes = "删除档案盒管理")
    @PreAuthorize("@ss.hasPermi('base:box:remove')")
    @Log(title = "档案盒管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{boxIds}")
    public AjaxResult remove(@PathVariable String[] boxIds) {
        for (String boxId : boxIds) {
            //查询档案盒是否被引用
            List<ArcArchive> arcArchiveList = arcArchiveService.selectArcArchiveList(new ArcArchive() {{
                setBoxId(boxId);
            }});
            if (arcArchiveList.size() > 0) {
                return AjaxResult.error("档案盒序号" + boxId + "被引用，无法删除！");
            }
        }

        return toAjax(arcBoxService.deleteArcBoxByBoxIds(boxIds));
    }

    /**
     * 下载模板
     */
    @ApiOperation(value = "下载模板", notes = "下载模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ArcBox> util = new ExcelUtil<ArcBox>(ArcBox.class);
        util.importTemplateExcel(response, "档案盒数据");
    }

    /**
     * 导入档案盒数据
     */
    @ApiOperation(value = "导入档案盒数据", notes = "导入档案盒数据")
    @Log(title = "档案盒管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('base:box:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ArcBox> util = new ExcelUtil<ArcBox>(ArcBox.class);
        List<ArcBox> boxList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = arcBoxService.importBox(boxList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 批量打印标签号
     */
    @ApiOperation(value = "批量打印标签号", notes = "批量打印标签号")
    @PreAuthorize("@ss.hasPermi('base:box:batchPrint')")
    @Log(title = "批量打印标签号", businessType = BusinessType.DELETE)
    @PostMapping("batchPrint/{boxIds}")
    public AjaxResult batchPrint(@PathVariable String[] boxIds) {
        List<ArcBox> arcBoxList;
        try {
            arcBoxList = arcBoxService.batchPrint(boxIds);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(arcBoxList);
    }

    /**
     * 绑定位置(选择位置)
     */
    @ApiOperation(value = "绑定位置(选择位置)", notes = "绑定位置(选择位置)")
    @PreAuthorize("@ss.hasPermi('base:box:editPosition')")
    @Log(title = "档案盒管理", businessType = BusinessType.UPDATE)
    @PostMapping("/editPosition")
    public AjaxResult editPosition(@RequestBody ArcBox arcBox) {
        if (null == arcBox.getBoxId()) {
            return AjaxResult.error("档案盒序号为空，请检查重试！");
        }
//        if(null == arcBox.getPositionType()){
//            return AjaxResult.error("位置类型为空请检查重试!");
//        }
//        if (StringUtils.isEmpty(arcBox.getShelvesCode())) {
//            return AjaxResult.error("位置信息为空，请检查重试！");
//        }
//        if (StringUtils.isEmpty(arcBox.getShelvesLayerGrid())) {
//            return AjaxResult.error("详细信息为空，请检查重试！");
//        }
        return toAjax(arcBoxService.editPosition(arcBox));
    }

    /**
     * 操作密集架
     */
    @PostMapping("/operationShelvesByBoxNo")
    public AjaxResult operationShelvesByBoxNo(@RequestBody ArcBox arcBox) {

        return toAjax(arcBoxService.operationShelvesByBoxNo(arcBox));
    }
}
